import delimited "./ipeds/2003/hd2003.csv", stringcols(12 13 14) clear 

gen year=2003

keep ein sector iclevel control hloffer hdegoffr deggrant carnegie

destring ein, generate(num_ein) force
drop if num_ein<0
drop if missing(num_ein)
drop num_ein

drop if deggrant==4

duplicates tag ein, gen(dup)
gen is_n_m3 = (carnegie!=-3)
bys ein: egen has_n_m3 = total(is_n_m3)
drop if has_n_m3>0 & is_n_m3==0 & dup>0
drop has_n_m3 is_n_m3 dup

duplicates drop
foreach num of numlist 15 16 21 22 31 {
duplicates tag ein, gen(dup)
gen is_`num' = (carnegie==`num')
bys ein: egen has_`num' = total(is_`num')
drop if has_`num'>0 & is_`num'==0 & dup>0
drop has_`num' is_`num' dup
}

duplicates drop
foreach num of numlist 1 2 {
duplicates tag ein, gen(dup)
gen is_`num' = (deggrant==`num')
bys ein: egen has_`num' = total(is_`num')
drop if has_`num'>0 & is_`num'==0 & dup>0
drop has_`num' is_`num' dup
}

foreach num of numlist 9(1)2 {
duplicates tag ein, gen(dup)
gen is_`num' = (hloffer==`num')
bys ein: egen has_`num' = total(is_`num')
drop if has_`num'>0 & is_`num'==0 & dup>0
drop has_`num' is_`num' dup
}

duplicates tag ein, gen(dup)
gen is_0 = (sector==0)
bys ein: egen has_0 = total(is_0)
drop if has_0>0 & is_0==0 & dup>0
drop has_0 is_0 dup

duplicates tag ein, gen(dup)
gen is_n_m3 = (hdegoffr!=-3)
bys ein: egen has_n_m3 = total(is_n_m3)
drop if has_n_m3>0 & is_n_m3==0 & dup>0
drop has_n_m3 is_n_m3 dup

foreach num of numlist 11 10 21 20 31 30 {
duplicates tag ein, gen(dup)
gen is_`num' = (hdegoffr==`num')
bys ein: egen has_`num' = total(is_`num')
drop if has_`num'>0 & is_`num'==0 & dup>0
drop has_`num' is_`num' dup
}

duplicates tag ein, gen(dup)
gen is_n_0 = (hdegoffr!=0)
bys ein: egen has_n_0 = total(is_n_0)
drop if has_n_0>0 & is_n_0==0 & dup>0
drop has_n_0 is_n_0 dup

duplicates tag ein, gen(dup)
gen is_n_3 = (control!=3)
bys ein: egen has_n_3 = total(is_n_3)
drop if has_n_3>0 & is_n_3==0 & dup>0
drop has_n_3 is_n_3 dup

foreach num of numlist 32 33 40 {
duplicates tag ein, gen(dup)
gen is_`num' = (carnegie==`num')
bys ein: egen has_`num' = total(is_`num')
drop if has_`num'>0 & is_`num'==0 & dup>0
drop has_`num' is_`num' dup
}

*Random
gen runiform = runiform()
sort ein carnegie runiform
duplicates drop ein, force
drop  runiform

label variable ein "Employer Identification Number"
label variable sector "Sector of institution"
label variable iclevel "Level of institution"
label variable control "Control of institution"
label variable hloffer "Highest level of offering"
label variable hdegoffr "Highest degree offered"
label variable deggrant "Degree granting status"
label variable carnegie "Carnegie Classification 2000"

label define label_sector 0 "Central office or Administrative Unit" 
label define label_sector 1 "4-year public", add 
label define label_sector 2 "4-year private, not-for-profit", add 
label define label_sector 3 "4-year private, for-profit", add 
label define label_sector 4 "2-year public", add 
label define label_sector 5 "2-year private, not-for-profit", add 
label define label_sector 6 "2-year private, for-profit", add 
label define label_sector 7 "Less than 2-year public", add 
label define label_sector 8 "Less than 2-year private, not-for-profit", add 
label define label_sector 9 "Less than 2-year private, for-profit", add 
label define label_sector 99 "sector not known", add 
label values sector label_sector

label define label_iclevel -3 "{Not available}" 
label define label_iclevel 1 "Four or more years", add 
label define label_iclevel 2 "At least 2 but less than 4 years", add 
label define label_iclevel 3 "Less than 2 years (below associate)", add 
label values iclevel label_iclevel

label define label_control -3 "{Not available}" 
label define label_control 1 "Public", add 
label define label_control 2 "Private, not-for-profit", add 
label define label_control 3 "Private, for-profit", add 
label values control label_control

label define label_hloffer -2 "{Not applicable, first-professional only}" 
label define label_hloffer -3 "{Not available}", add 
label define label_hloffer 0 "Other", add 
label define label_hloffer 1 "Award of less than one academic year", add 
label define label_hloffer 2 "At least 1, but less than 2 academic yea", add 
label define label_hloffer 3 "Associates degree", add 
label define label_hloffer 4 "At least 2, but less than 4 academic yea", add 
label define label_hloffer 5 "Bachelors degree", add 
label define label_hloffer 6 "Postbaccalaureate certificate", add 
label define label_hloffer 7 "Masters degree", add 
label define label_hloffer 8 "Post-masters certificate", add 
label define label_hloffer 9 "Doctors degree", add 
label values hloffer label_hloffer

label define label_hdegoffr 0 "Non-degree granting"
label define label_hdegoffr 1 "First-professional only",add
label define label_hdegoffr 10 "Doctoral",add
label define label_hdegoffr 11 "Doctoral and first-professional",add
label define label_hdegoffr 20 "Masters",add
label define label_hdegoffr 21 "Masters and first-professional",add
label define label_hdegoffr 30 "Bachelors",add
label define label_hdegoffr 31 "Bachelors and first-professional",add
label define label_hdegoffr 40 "Associates",add
label define label_hdegoffr -3 "{Not available}",add
label values hdegoffr label_hdegoffr

label define label_deggrant -3 "{Not available}" 
label define label_deggrant 1 "Degree-granting", add 
label define label_deggrant 2 "Nondegree-granting, primarily postsecond", add 
label define label_deggrant 3 "Not primarily postsecondary institutions", add 
label define label_deggrant 4 "Institution is not an educational entity", add 
label values deggrant label_deggrant

label define label_carnegie 15 "Doctoral/Research Universities--Extensive"
label define label_carnegie 16 "Doctoral/Research Universities--Intensive",add
label define label_carnegie 21 "Masters Colleges and Universities I",add
label define label_carnegie 22 "Masters Colleges and Universities II",add
label define label_carnegie 31 "Baccalaureate Colleges--Liberal Arts",add
label define label_carnegie 32 "Baccalaureate Colleges--General",add
label define label_carnegie 33 "Baccalaureate/Associates Colleges",add
label define label_carnegie 40 "Associates Colleges",add
label define label_carnegie 51 "Theological seminaries and other specialized faith-related institutions",add
label define label_carnegie 52 "Medical schools and medical centers",add
label define label_carnegie 53 "Other separate health profession schools",add
label define label_carnegie 54 "Schools of engineering and technology",add
label define label_carnegie 55 "Schools of business and management",add
label define label_carnegie 56 "Schools of art, music, and design",add
label define label_carnegie 57 "Schools of law",add
label define label_carnegie 58 "Teachers colleges",add
label define label_carnegie 59 "Other specialized institutions",add
label define label_carnegie 60 "Tribal colleges",add
label define label_carnegie -3 "{Item not available}",add
label values carnegie label_carnegie

save "./ipeds/2003/uni2003", replace

